package com.xnyzc.lhy.order.controller.dapi.driver;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.order.EOrderStatus;
import com.xnyzc.lhy.common.util.AssertUtil;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.config.StsServiceSampleConfig;
import com.xnyzc.lhy.order.entity.param.cms.PageDto;
import com.xnyzc.lhy.order.entity.param.order.CharteredDetailsParam;
import com.xnyzc.lhy.order.entity.param.order.DisBookingOrderParam;
import com.xnyzc.lhy.order.service.work.IOaDWorkStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @Description 司机获取信息
 * @Author zhuyubin
 * @Date 2019/8/3 11:06
 **/
@Api(tags = "订单接口")
@RestController
@RequestMapping(value = "dpi/driverWorkStatistics")
public class DriverWorkStatisticsController {

    @Autowired
    private IOaDWorkStatisticsService iOaDWorkStatisticsService;

    @GetMapping(value = "getDriverWorkStatistics")
    public Rv getDriverWorkStatistics() {
        Long driverId = TokenUtil.getTokenUserId();
        if (driverId == null) {
            throw PangException.create(EErrorCode.tokenExpire);
        }
        String tag = TokenUtil.getTag();
        String userType = TokenUtil.getUserType();
        if (CheckUtil.strIsEmpty(tag, userType)) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaDWorkStatisticsService.getWorkStatistics(driverId, tag, Integer.valueOf(userType)));
    }

    /***
     *  获取OSS中STS的临时令牌
     * @return
     */
    @ApiOperation(value = "获取OSS中STS的临时令牌", notes = "获取OSS中STS的临时令牌")
    @PostMapping(value = "getSTSTemporaryToken")
    public Rv getSTSTemporaryToken() {
        //考虑安全问题--验证一下
        Long driverId = TokenUtil.getTokenUserId();
        if (driverId == null) {
            throw PangException.create(EErrorCode.tokenExpire);
        }
        String tag = TokenUtil.getTag();
        String userType = TokenUtil.getUserType();
        if (CheckUtil.strIsEmpty(tag, userType)) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        //获取sts的安全令牌具体实现方法--得到返回结果
        String res = StsServiceSampleConfig.getStsServiceSample();
        if (CheckUtil.objIsEmpty(res)) {
            return Rv.error(EErrorCode.stsTemporaryTokenIsNull);
        }
        return Rv.wrap(res);
    }

    /**
     * 首页消息列表
     *
     * @param pageDto 分页
     * @return
     */
    @PostMapping(value = "/getDriverHomeMessage")
    public Rv getDriverHomeMessage(@RequestBody PageDto pageDto) {
        return Rv.wrap(iOaDWorkStatisticsService.getHomeMessage(pageDto));
    }

    /**
     * 甩单至供应商订单大厅
     *
     * @param orderId 订单ID
     * @return
     */
    @GetMapping(value = "/throwOrderToHall")
    public Rv throwOrderToHall(@RequestParam("orderId") String orderId) {
        return Rv.wrap(iOaDWorkStatisticsService.throwOrderToHall(orderId));
    }

    /**
     * 获取预约订单信息
     *
     * @param cityId 城市Id
     * @return
     */
    @GetMapping(value = "/getBookingOrderMessage")
    public Rv getBookingOrderMessage(@RequestParam("cityId") String cityId, @RequestParam("officeId") Long officeId) {
        return Rv.wrap(iOaDWorkStatisticsService.getBookingOrder(cityId, officeId));
    }

    /**
     * 抢单
     *
     * @param orderId 订单ID
     * @return
     */
    @GetMapping(value = "/robBookingOrder")
    public Rv robBookingOrder(@RequestParam("orderId") String orderId) {
        Long driverId = TokenUtil.getTokenUserId();
        if (driverId == null) {
            throw PangException.create(EErrorCode.tokenExpire);
        }
        return iOaDWorkStatisticsService.robBookingOrder(driverId, orderId);
    }

    /**
     * 派单
     *
     * @param param 订单ID
     * @return
     */
    @PostMapping(value = "/disBookingOrder")
    public Rv disBookingOrder(@RequestBody DisBookingOrderParam param) {
        return iOaDWorkStatisticsService.disBookingOrder(param);
    }

    /**
     * 派单
     *
     * @param param 订单ID
     * @return
     */
    @PostMapping(value = "assignCharterDriver")
    public Rv assignCharterDriver(@RequestBody(required = false) CharteredDetailsParam charteredDetailsParam){
        if(CheckUtil.objIsNotEmpty(charteredDetailsParam)){
            return iOaDWorkStatisticsService.assignCharterDriver(charteredDetailsParam);
        }
        return Rv.wrap();
    }

    /**
     * 挂起订单
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/suspendOrder")
    public Rv suspendOrder(@RequestParam("orderNo") String orderNo) {
        return iOaDWorkStatisticsService.suspendOrder(orderNo);
    }

    /**
     * 改派供应商
     *
     * @param param 改派信息
     * @return
     */
    @PostMapping(value = "/throwBookingOrder")
    public Rv throwBookingOrder(@RequestBody DisBookingOrderParam param) {
        return iOaDWorkStatisticsService.throwBookingOrder(param);
    }

    /**
     * 公司改派供应商
     *
     * @param param 改派信息
     * @return
     */
    @PostMapping(value = "/disBookingOrderToManager")
    public Rv disBookingOrderToManager(@RequestBody DisBookingOrderParam param) {
        return iOaDWorkStatisticsService.disBookingOrderToManager(param);
    }

    /**
     * 开始行程-司机端
     *
     * @param orderNo 订单ID
     * @return
     */
    @GetMapping(value = "/startMyBookingOrder")
    public Rv startMyBookingOrder(@RequestParam("orderNo") String orderNo) {
        Long driverId = TokenUtil.getTokenUserId();
        if (driverId == null) {
            throw PangException.create(EErrorCode.tokenExpire);
        }
        return iOaDWorkStatisticsService.startMyBookingOrder(driverId, orderNo, EOrderStatus.reservationLocation);
    }

    /**
     * 改派订单-司机端
     *
     * @param orderNo 订单ID
     * @return
     */
//    @GetMapping(value = "/throwMyBookingOrder")
//    public Rv throwMyBookingOrder(@RequestParam("orderNo") String orderNo) {
//        return iOaDWorkStatisticsService.throwMyBookingOrder(orderNo);
//    }

    @GetMapping("/order/accept")
    @ApiOperation(value = "供应商接单", notes = "供应商接单")
    public Rv orderAccept(@RequestParam String orderNo) {
        AssertUtil.notBlank(orderNo, "订单号不能为空");
        return Rv.wrap(iOaDWorkStatisticsService.orderAccept(orderNo));
    }

    @GetMapping("/order/tracking")
    @ApiOperation(value = "订单跟踪记录", notes = "订单跟踪记录")
    public Rv orderTracking(@RequestParam String orderNo) {
        AssertUtil.notBlank(orderNo, "订单号不能为空");
        return Rv.wrap(iOaDWorkStatisticsService.orderTracking(orderNo));
    }
}
